package com.grs.demo.download;

import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.grs.demo.R;

import org.xutils.common.Callback;
import org.xutils.download.DownloadInfo;
import org.xutils.download.DownloadManager;
import org.xutils.download.DownloadService;
import org.xutils.download.DownloadState;
import org.xutils.download.DownloadViewHolder;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.File;

public class DownloadItemViewHolder extends DownloadViewHolder {
    private DownloadManager downloadManager;
    @ViewInject(R.id.download_label)
    TextView label;
    @ViewInject(R.id.download_state)
    TextView state;
    @ViewInject(R.id.download_pb)
    ProgressBar progressBar;
    @ViewInject(R.id.download_stop_btn)
    Button stopBtn;

    public DownloadItemViewHolder(View view, DownloadInfo downloadInfo) {
        super(view, downloadInfo);
        if (downloadManager == null) {
            downloadManager = DownloadService.getDownloadManager();
        }
        refresh();
    }

    @Event(R.id.download_stop_btn)
    private void toggleEvent(View view) {
        DownloadState state = downloadInfo.getState();
        switch (state) {
            case WAITING:
            case STARTED:
                downloadManager.stopDownload(downloadInfo);
                break;
            case ERROR:
            case STOPPED:
                try {
                    downloadManager.startDownload(
                            downloadInfo.getUrl(),
                            downloadInfo.getLabel(),
                            downloadInfo.getFileSavePath(),
                            downloadInfo.isAutoResume(),
                            downloadInfo.isAutoRename(),
                            this);
                } catch (DbException ex) {
                    Toast.makeText(x.app(), "添加下载失败", Toast.LENGTH_LONG).show();
                }
                break;
            case FINISHED:
                Toast.makeText(x.app(), "已经下载完成", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }

    @Event(R.id.download_remove_btn)
    private void removeEvent(View view) {
        try {
            downloadManager.removeDownload(downloadInfo);
//                downloadListAdapter.notifyDataSetChanged();
        } catch (DbException e) {
            Toast.makeText(x.app(), "移除任务失败", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void update(DownloadInfo downloadInfo) {
        super.update(downloadInfo);
        refresh();
    }

    @Override
    public void onWaiting() {
        refresh();
    }

    @Override
    public void onStarted() {
        refresh();
    }

    @Override
    public void onLoading(long total, long current) {
        refresh();
    }

    @Override
    public void onSuccess(File result) {
        refresh();
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        refresh();
    }

    @Override
    public void onCancelled(Callback.CancelledException cex) {
        refresh();
    }

    public void refresh() {
        label.setText(downloadInfo.getLabel());
        state.setText(downloadInfo.getState().toString());
        progressBar.setProgress(downloadInfo.getProgress());

        stopBtn.setVisibility(View.VISIBLE);
        stopBtn.setText(x.app().getString(R.string.stop));
        DownloadState state = downloadInfo.getState();
        switch (state) {
            case WAITING:
            case STARTED:
                stopBtn.setText(x.app().getString(R.string.stop));
                break;
            case ERROR:
            case STOPPED:
                stopBtn.setText(x.app().getString(R.string.start));
                break;
            case FINISHED:
                stopBtn.setVisibility(View.INVISIBLE);
                break;
            default:
                stopBtn.setText(x.app().getString(R.string.start));
                break;
        }
    }
}